package com.example.listviewtest;

import android.app.ListActivity;
import android.app.LoaderManager;
import android.content.CursorLoader;
import android.content.Loader;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.Gravity;
import android.view.Menu;
import android.view.ViewGroup;
import android.widget.AbsListView.LayoutParams;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.SimpleCursorAdapter;

public class MainActivity extends ListActivity implements
		LoaderManager.LoaderCallbacks<Cursor> {
	SimpleCursorAdapter mAdapter;

	//Add a new comment Line
	// These are the Contacts rows that we will retrieve
	static final String[] PROJECTION = new String[] {
			ContactsContract.Data._ID, ContactsContract.Data.DISPLAY_NAME };

	// This is the select criteria
	static final String SELECTION = "((" + ContactsContract.Data.DISPLAY_NAME
			+ " NOTNULL) AND (" + ContactsContract.Data.DISPLAY_NAME + " !=''))";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
//		 setContentView(R.layout.activity_main);
		
		//Create a progress bar to display while the list loads
		ProgressBar progressBar=new ProgressBar(this);
		progressBar.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT, Gravity.CENTER));
		progressBar.setIndeterminate(true);
		
		ListView listView=getListView();
		listView.setEmptyView(progressBar);
		
		//Must add the progress bar to the root of the layout
		ViewGroup root=(ViewGroup)findViewById(android.R.id.content);
		root.addView(progressBar);
		
		//For the cursor adapter, specify which columns go into which views
		String[] fromColumns={ContactsContract.Data.DISPLAY_NAME};
		int [] toViews={android.R.id.text1};//The 
		
		//Create an empty adapter we will use to display the loaded data.
		//We pass null for the cursor, then update it in onLoadFinished();
		mAdapter=new SimpleCursorAdapter(this,android.R.layout.simple_list_item_1,null,fromColumns,toViews,0);
		
		setListAdapter(mAdapter);
		
		//Prepare the loader. Either re-connect with an existing one,
		//or start a new one.
		getLoaderManager().initLoader(0, null, this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	@Override
	public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
		// TODO Auto-generated method stub
		//Now create and return a CursorLoader that will take care of 
		//creating a Cursor for the data being displayed
		return new CursorLoader(this,ContactsContract.Data.CONTENT_URI,PROJECTION, SELECTION, null, null);
	}

	//Called when a new Loader needs to be created
	@Override
	public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
		// TODO Auto-generated method stub
		//Swap the new cursor in.
		mAdapter.swapCursor(data);

	}

	@Override
	public void onLoaderReset(Loader<Cursor> arg0) {
		// TODO Auto-generated method stub
		mAdapter.swapCursor(null);
	}
}
